package com.imagination.applet.infrastructure.process;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

@Slf4j
public class PythonCommand {
    private final Runtime runtime;

    private PythonCommand() {
        runtime = Runtime.getRuntime();
        ;
    }

    public void runAndWait(String[] arguments) throws Exception {
        Process p = runtime.exec(arguments);
        while (p.isAlive()) {
            p.waitFor(10, TimeUnit.MILLISECONDS);
        }
        ProcessBuilder pb = new ProcessBuilder(arguments);
        BufferedReader error = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = error.readLine()) != null) {
            log.warn(line);
        }
    }

    public static PythonCommand getInstance() {
        return new PythonCommand();
    }

}
